package com.personal.oa.until;

import com.google.gson.Gson;
import org.slf4j.Logger;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018/1/26.
 */
public class Unitl {

    public Date convertDate(String datastr, String dbouuid, Logger logger) {
        int datel = datastr.length();
        Date dt = null;
        try {
            if (datastr.indexOf("1900-01-01") == -1) {
                if (datastr.indexOf("-") != -1) {
                    if (datel > 9) {
                        //这里要标准的年月日
                        dt = new SimpleDateFormat("yyyy-MM-dd").parse(datastr.substring(0, 10));
                    } else if (datel > 6 && datel < 9) {
                        //这里要自己补充日子
                        dt = new SimpleDateFormat("yyyy-MM-dd").parse(datastr + "-01");
                    }
                } else {
                    if (4 == datel) {
                        //这里要自己补充月和日
                        dt = new SimpleDateFormat("yyyyMMdd").parse(datastr + "0101");
                    } else if (8 == datel) {
                        //这里要自己补充日子
                        dt = new SimpleDateFormat("yyyyMMdd").parse(datastr);
                    }
                }
            }
        } catch (ParseException pe) {
            logger.info(dbouuid + ":创建时间异常读取:" + datastr + ";长度:" + (dt));
            logger.info("ParseException thrown  pe:" + pe + "\n");
        }
        return dt;
    }

    /**
     * 数据匹配转换
     *
     * @param Identification 辨识参数
     * @return
     */
    public Map<String, List> DataMatchTransformation(String Identification) throws FileNotFoundException {
//        Map<String, String> jm = new HashMap<>();
//        JsonParser parser = new JsonParser();
        String filepath = "";
        if ("language".equals(Identification)) {
            filepath = "/static/json/language.json";
        }
//        else if("languagecn".equals(Identification)){
//            filepath = "/static/json/languagecn.json";
//        }

        else if ("dname".equals(Identification)) {
            filepath = "/static/json/obelongsto.json";
        }
//          else if("publishers".equals(Identification)){
//            filepath = "/static/json/publishers.json";
//        }else if("subjects".equals(Identification)){
//            filepath = "/static/json/subjects.json";
//        }

        InputStream is = this.getClass().getResourceAsStream(filepath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Gson gson = new Gson();
        HashMap map = gson.fromJson(sb.toString(), HashMap.class);
//        JsonObject object = (JsonObject) parser.parse(sb.toString());
//        for (String s : object.keySet()) {
//            //语言数组
//            JsonArray jas = object.getAsJsonArray(s);
//            if ("languagecn".equals(Identification)) {
//                for (JsonElement ja : jas) {
//                    jm.put(((JsonObject) ja).get("name").getAsString(), ((JsonObject) ja).get("namecn").getAsString());
//                }
//
//            } else {
//                for (JsonElement ja : jas) {
//                    jm.put(((JsonObject) ja).get("name").getAsString(), s);
//                }
//
//            }
//        }

        return map;

    }

    //博硕中英对照
    public String dnameCheck(String name) {
        String cn = "";
        switch (name) {
            case "Bachelor":
                cn = "学士";
                break;
            case "Master":
                cn = "硕士";
                break;
            case "Doctor":
                cn = "博士";
                break;
            case "Other":
                cn = "其他";
                break;
        }
        return cn;
    }

    //匹配期刊的卷、期、页
    public String matchingJournal(String regex, String result) {
        Matcher matcher = Pattern.compile(regex).matcher(result);
        return matcher.find() ? matcher.group(1) : null;
    }

//    public static void main(String[] args) { //MATEC Web of Conferences, Vol 8, p 02012 (2013)
//        Unitl unitl=new Unitl();//Nonlinear Processes in Geophysics, Vol 13, Iss 5, Pp 571-576 (2006)
//        String result = unitl.matchingJournal("Vol (.*?)(,| )", "Frontiers in Plant Science, Vol 3,Iss ");
//        System.out.println(result);
//    }
}
